package com.css.apps.base.user.action;

import com.css.apps.base.common.BaseEnvironment;
import com.css.apps.base.org.model.SOrg;
import com.css.apps.base.user.model.SUser;
import com.css.core.configuration.ConfigurationManager;
import com.css.core.configuration.Environment;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.page.Page;
import com.css.db.query.QueryCache;
import com.css.util.ListUtil;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("serial")
public class DirUser extends AdminAction {

	private static Log log = LogFactory.getLog(DirUser.class);
	private String username;
	private String openFlag;
	private String orgId;
	private String includeFlag = "1";  //1:当前组织  2：包含下级
	private String activeStatus;
	private String isBranch = "N";
	private List<String> orgIdList = new ArrayList<String>();
	private Page page;

	public DirUser() {
		page = new Page();
		page.setCountField("a.uuid");
	}

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		try {
			Map paramMap = obtainOrgIdMap();
			this.orgId = (String) paramMap.get("curOrgId");
			orgIdList = (List<String>) paramMap.get("childOrgIdWithSelf");
			QueryCache qc = new QueryCache("select a.uuid from SUser a " + getWhere() + getOrder());
			setWhere(qc);
			page = qc.page(page);
			page.setResults(QueryCache.idToObj(SUser.class, page.getResults()));
			return SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			setMessage(Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	private Map<String,Object> obtainOrgIdMap(){
		Map<String,Object> map = new HashMap();
		Tree tree = new TreeCache(SOrg.class);
		TreeNode curNode = StringHelper.isNotEmpty(orgId) ?
				tree.getTreeNode(orgId) : ("Y".equalsIgnoreCase(isBranch) ? tree.getTreeNode(sUser.getOrganId()) : tree.getRoot().get(0));
		map.put("curOrgId",curNode.getNodeId());
		if("1".equals(includeFlag)){
			//点击当前组织情况，注入当前节点的ID
			orgIdList.add(curNode.getNodeId());
		}else{
			//点击包含下级情况,注入当前节点ID和全部子节点ID
			orgIdList.add(curNode.getNodeId());
			for (TreeNode node : curNode.getAllChildren()) {
				orgIdList.add(node.getNodeId());
			}
		}
		map.put("childOrgIdWithSelf",orgIdList);
		return map;
	}

	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where a.delFlag='" + Environment.UN_DELETE_STATE + "'");
		if (ListUtil.isLstNotEmpty(orgIdList)) {
			sb.append(" and exists(select 1 from SUserOrg uo where uo.userId=a.uuid and  uo.deptId in(:orgIdList) ) ");
		}
		// run模式下只能显示普通用户
		if (!ConfigurationManager.isAdminMode()) {
			sb.append(" and a.userType = '" + BaseEnvironment.USERTYPE_NORMAL + "' ");
		}
		if (StringHelper.isNotEmpty(openFlag))
			sb.append(" and a.openFlag = :openFlag ");
		if (StringHelper.isNotEmpty(activeStatus))
			sb.append(" and a.activeStatus = :activeStatus ");
		if (StringHelper.isNotEmpty(username))
			sb.append(" and (a.loginName like :name or a.realName like :name) ");
		return sb.toString();
	}

	public void setWhere(QueryCache qc) {
		if (ListUtil.isLstNotEmpty(orgIdList)) {
			qc.setParameter("orgIdList", orgIdList);
		}
		if (StringHelper.isNotEmpty(username))
			qc.setParameter("name", "%" + username.trim() + "%");
		if (StringHelper.isNotEmpty(openFlag))
			qc.setParameter("openFlag", openFlag.trim());
		if (StringHelper.isNotEmpty(activeStatus))
			qc.setParameter("activeStatus", activeStatus.trim());
	}

	public String getOrder() {
		return StringHelper.isNotEmpty(page.getOrderByString()) ? page.getOrderByString() : "order by a.orderNum,a.lastmodifytime ";
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getOpenFlag() {
		return openFlag;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setOpenFlag(String openFlag) {
		this.openFlag = openFlag;
	}

	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	public String getIncludeFlag() {
		return includeFlag;
	}

	public void setIncludeFlag(String includeFlag) {
		this.includeFlag = includeFlag;
	}


	public String getActiveStatus() {
		return activeStatus;
	}

	public void setActiveStatus(String activeStatus) {
		this.activeStatus = activeStatus;
	}

	public String getIsBranch() {
		return isBranch;
	}

	public void setIsBranch(String isBranch) {
		this.isBranch = isBranch;
	}



}
